
import chain.IDialogEventHandler;
import cmd.DatasetViewerCmd;
import json.ElementJson;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author dakhoa
 * @version 1.0
 */
public class DatasetViewerController extends AbstractController {

    private ElementJson table;

    @Override
    public ModelAndView showView(HttpServletRequest request, HttpServletResponse response) {
        Map flowScope = getFlowScope();

        int random;
        if (flowScope.get("flowScopeData") == null) {
            random = (int) (Math.random() * 10);

        } else {
            random = Integer.valueOf((Integer) flowScope.get("flowScopeData"));
            random++;
        }

        flowScope.put("flowScopeData", random);

        if (!flowScope.containsKey("localeList")) {
            flowScope.put("localeList", Locale.getAvailableLocales());
        }

        return mvTemplate();
    }

    @Override
    protected Object newCommandObject(Class clazz) throws Exception {
        DatasetViewerCmd cmd = (DatasetViewerCmd) getFlowScope().get("cmd");
        if (cmd == null) {

            cmd = (DatasetViewerCmd) super.newCommandObject(clazz);
            cmd.setSelectedPrintSave(new ElementJson("seletedPrintSave"));
            cmd.setPsCSV(new ElementJson("psCSV"));
            cmd.setPsPDFLandscape(new ElementJson("psPDFLandscape"));
            cmd.setPsPDFPortrait(new ElementJson("psPDFPortrait"));
            cmd.setPsTEXT(new ElementJson("psTEXT"));

            getFlowScope().put("cmd", cmd);
        }

        return cmd;
    }

    public void onPrintSaveSelect(HttpServletRequest request, HttpServletResponse response, DatasetViewerCmd cmd) {
        ElementJson e = cmd.getSelectedPrintSave();

        String printSaveOption = request.getParameter("printSaveOption");
        e.setValue(printSaveOption);
        e.setDisabled(printSaveOption.equals("CSV") ? "true" : "false");
        e.setVisibled(printSaveOption.equals("TEXT") ? "false" : "true");

        if (printSaveOption.equals("CSV")) {
            if (cmd.getPsCSV().getChecked().equals("true")) {
                cmd.getPsCSV().setDisabled("false");
                cmd.getPsCSV().setChecked("false");

            } else {
                cmd.getPsCSV().setChecked("true");
                cmd.getPsCSV().setDisabled("true");
            }

        } else if (printSaveOption.equals("TEXT")) {
            cmd.getPsTEXT().setChecked(cmd.getPsTEXT().getChecked().equals("true") ? "false" : "true");

        } else if (printSaveOption.equals("PDF Landscape")) {
            cmd.getPsPDFLandscape().setChecked(cmd.getPsPDFLandscape().getChecked().equals("true") ? "false" : "true");

        } else {
            cmd.getPsPDFPortrait().setChecked(cmd.getPsPDFPortrait().getChecked().equals("true") ? "false" : "true");
        }


        if (getFlowScope().containsKey("seletedPrintSavePre")) {
            e = new ElementJson("seletedPrintSavePre", null, null, null);
            e.setValue((String) getFlowScope().get("seletedPrintSavePre"));
        }

        getFlowScope().put("seletedPrintSavePre", printSaveOption);

        table = new ElementJson("table1", null, null, "false");

        ElementJson totalMsg = new ElementJson("totalRowMsg", null, null, "true");

        totalMsg.setInnerHTML(Math.random() + " - " +((Locale[]) getFlowScope().get("localeList")).length + " rows unfiltered");

        registerJson(
            cmd.getSelectedPrintSave(),
            cmd.getPsCSV(),
            cmd.getPsPDFLandscape(),
            cmd.getPsPDFPortrait(),
            cmd.getPsTEXT(), e, table, totalMsg);

        unRegisterJson(table);
    }

    public void printSave(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.getWriter().write(getFlowScope().toString());
            System.out.println(getFlowScope());

            response.getWriter().flush();
        } catch (IOException ex) {
            Logger.getLogger(DatasetViewerController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ModelAndView newQuery(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = mvTemplate();
        mv.addObject("name", "value");
        return mv;
    }

    public void doFilter(HttpServletRequest request, HttpServletResponse response) {
        super.handleDialog(new IDialogEventHandler() {

            public boolean handleDialogOpen(Map<String, Object> dialogData, List<String> errs) {
                return true;
            }

            public void handleDialogClose(Map<String, Object> dialogData) {
                System.out.println("Dialog return data: " + dialogData);
                System.out.println("Current return data: " + getFlowScope());
            }
        });
    }

    public ModelAndView next(HttpServletRequest request, HttpServletResponse response) {
        return mvTemplate();
    }

    public void openBundleOwner(HttpServletRequest request, HttpServletResponse response) {
        super.handleDialog(new IDialogEventHandler() {

            public boolean handleDialogOpen(Map<String, Object> dialogData, List<String> errs) {
                boolean result = true;

                //perform validation;
                if (true) { //assume failed
//                params.put("name", "khoa");
                    result = true;
                }

                return result;
            }

            public void handleDialogClose(Map<String, Object> dialogData) {
                System.out.println("Dialog return data: " + dialogData);
                System.out.println("Current return data: " + getFlowScope());
            }
        });
    }
}
